//
//  ViewController.swift
//  自定义表情键盘
//
//  Created by YYY on 2017/9/5.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    //文本视图
    @IBOutlet weak var textInputView: UITextView!
    
    lazy var emoticonView: EmoticonInputView = EmoticonInputView.emoticonView { [weak self] (emoticonModel) in
        
        self?.insertEmoticonModel(model: emoticonModel)
    }
    
    func insertEmoticonModel(model: CPJEmoticonModel?)
    {
        guard let emoticonModel = model else
        {
            //删除文本按钮
            textInputView.deleteBackward()
            return
        }
        
        //emoji表情按钮
        if let emoji = emoticonModel.emoji,
            let textRange = textInputView.selectedTextRange
        {
            textInputView.replace(textRange, withText: emoji)
            return
        }
        
        //获取表情中的图像属性文本
        let imageText = emoticonModel.imageText(font: textInputView.font!)
        //获取textView的可变属性文本
        let attributeText = NSMutableAttributedString(attributedString: textInputView.attributedText)
        //将图像的属性文本，插入光标的当前位置
        attributeText.replaceCharacters(in: textInputView.selectedRange, with: imageText)
        
        //记录光标位置
        let range = textInputView.selectedRange
        
        //重新设置属性文本
        textInputView.attributedText = attributeText
        
        //恢复光标位置
        textInputView.selectedRange = NSRange(location: range.location + 1, length: 0)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置输入视图
        textInputView.inputView = emoticonView
        
        //刷新键盘输入视图
        textInputView.reloadInputViews()
        
        test()
    }

    
    @IBAction func showAction(_ sender: Any)
    {
        print(emoticonText)
       
        
        
    }
    
    
    var emoticonText: String?
    {
        //获取textview的属性文本
        guard let attriText = textInputView.attributedText else
        {
            return ""
        }
        
        var result = String()
        
        //需要获的属性文本中的图片
        //遍历的范围、遍历的选项、闭包
        attriText.enumerateAttributes(in: NSRange(location: 0, length: attriText.length), options: []) { (dict, range, _) in
            
            print(dict)
            print(range)
            
            if let attachment = dict["NSAttachment"] as? EmoticonAttachment
            {
                result += attachment.chs ?? ""
                //图片文本
                print(attachment)
            }
            else
            {
                //纯文本 
                let subString = (attriText.string as NSString).substring(with: range)
                result += subString
            }
            
        }
        
        print(result)
        return attriText.string
    }
    
    func test()
    {
        //将一个十六进制的字符串转换成表情字符
        //unicode编码
        let code = "0x1f634"
        //实例化字符扫描
        let scanner = Scanner(string: code)
        
        //从code中扫描出十六进制的数值
        var result: UInt32 = 0
        
        scanner.scanHexInt32(&result)
        
        //使用Uint32的数值，生成一个UTF8的字符
        let c = Character(UnicodeScalar(result)!)
        
        let emoji = String(c)
        
        print("result = \(result)")
        
        print("c = \(c)")
        
        print("emoji = \(emoji)")
    }
    //呼出键盘
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        textInputView.becomeFirstResponder()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

